package cn.interestingshop.web.client;

import cn.interestingshop.entity.Classify;
import cn.interestingshop.entity.Notice;
import cn.interestingshop.entity.User;
import cn.interestingshop.service.classify.ClassifyService;
import cn.interestingshop.service.classify.ClassifyServiceImpl;
import cn.interestingshop.service.notice.NoticeService;
import cn.interestingshop.service.notice.NoticeSserviceImpl;
import cn.interestingshop.service.user.UserService;
import cn.interestingshop.service.user.UserServiceImpl;
import cn.interestingshop.utils.*;
import cn.interestingshop.web.AbstractServlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "LoginServlet", urlPatterns = {"/home", "/login", "/register"})
public class LoginServlet extends AbstractServlet {
    @Override
    public Class getServletClass() {
        return LoginServlet.class;
    }

    private UserService userService;

    @Override
    public void init() throws ServletException {
        userService = new UserServiceImpl();
    }

    public String index(HttpServletRequest request, HttpServletResponse response) {

        if (JedisUtil.getJedis().get("classifyVoList") != null) {
            List<ClassifyVo> list = JSONArray.parseArray(JedisUtil.getJedis().get("classifyVoList"), ClassifyVo.class);


            request.setAttribute("classifyVoList", list);
        } else {


            ClassifyService classifyService = new ClassifyServiceImpl();

            List<ClassifyVo> list = classifyService.selectAll();


            request.setAttribute("classifyVoList", list);

            JedisUtil.getJedis().set("classifyVoList", JSONArray.toJSONString(list));


        }

        if (JedisUtil.getJedis().get("notice") != null) {
            List<Notice> list1 = JSONArray.parseArray(JedisUtil.getJedis().get("notice"), Notice.class);

            request.setAttribute("notice", list1);
        } else {
            NoticeService noticeService = new NoticeSserviceImpl();
            List<Notice> list1 = noticeService.selectAll();

            request.setAttribute("notice", list1);
            JedisUtil.getJedis().set("notice", JSONArray.toJSONString(list1));
        }

        JedisUtil.close(JedisUtil.getJedis());




        return "client/index";
    }

    public String toLogin(HttpServletRequest request, HttpServletResponse response) {
        return "client/login";
    }

    public String toRegister(HttpServletRequest request, HttpServletResponse response) {
        return "client/register";
    }

    public String toYzmYm(HttpServletRequest request, HttpServletResponse response) {
        return "client/loginYzm";
    }


    public void login(HttpServletRequest request, HttpServletResponse response) {


        String account = request.getParameter("account");
        String password = Md5Util.md5(request.getParameter("password"));
        System.out.println(password);

        UserService userService = new UserServiceImpl();

        int id = -1;

        id = userService.selectOne(account, password);

        JSONObject jsonObject = new JSONObject();

        if (id > 0 && !EmptyUtils.isEmpty(id)) {
            HttpSession httpSession = request.getSession();


            User user = new User();

            user = userService.selectOne(id);

            httpSession.setAttribute("loginUser", user);

            Cookie cookie=new Cookie("loginUser", JSONObject.toJSONString(user));

            jsonObject.put("status", 1);
            jsonObject.put("message", "登陆成功!");


        } else {
            jsonObject.put("status", 0);
            jsonObject.put("message", "登陆失败!");
        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public void loginYzm(HttpServletRequest request, HttpServletResponse response) {


        System.out.println("awdawd");


        String yzm = request.getParameter("yzm");
        String phone = request.getParameter("phone");

        UserService userService = new UserServiceImpl();

        int id = -1;

        id = userService.selectOne(phone);

        JSONObject jsonObject = new JSONObject();

        if (id > 0 && !EmptyUtils.isEmpty(id)) {

            System.out.println(id);


            try {


                String yzm2 = String.valueOf(request.getSession().getAttribute(phone));

                if (Integer.parseInt(yzm2) == Integer.parseInt(yzm)) {
                    HttpSession httpSession = request.getSession();

                    User user = new User();

                    user = userService.selectOne(id);

                    httpSession.setAttribute("loginUser", user);

                    jsonObject.put("status", 1);
                    jsonObject.put("message", "登陆成功!");
                } else {
                    jsonObject.put("status", 0);
                    jsonObject.put("message", "验证码错误!");
                }

            } catch (Exception e) {
                jsonObject.put("status", 0);
                jsonObject.put("message", "请重新发送验证码!!");
            }


        } else {
            jsonObject.put("status", 0);
            jsonObject.put("message", "登陆失败!");
        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public void loginYzmType(HttpServletRequest request, HttpServletResponse response) {
        String phone = request.getParameter("phone");

        UserService userService = new UserServiceImpl();

        boolean ok = false;

        ok = userService.selectOnePhone(phone);

        JSONObject jsonObject = new JSONObject();

        if (ok == true) {


            YzmUtils yzmUtils = new YzmUtils();

            int rs = yzmUtils.yzm(request, response);

            if (rs == -1) {
                jsonObject.put("status", 0);
                jsonObject.put("message", "网络错误!");
            } else {
                jsonObject.put("status", 1);

            }


        } else {
            jsonObject.put("status", 0);
            jsonObject.put("message", "此电话号码未注册!");
        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public String loginOut(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute("loginUser");
        request.getSession().removeAttribute("recentGoods");
        return "index";
    }


    public void saveUserToDatabase(HttpServletRequest request, HttpServletResponse response) {
        UserService userService = new UserServiceImpl();

        User user = new User();
        int insert = 0;

        Boolean account = false;
        Boolean phone = false;

        user.setPassword(request.getParameter("password"));
        user.setPhone(request.getParameter("phone"));
        user.setNickName(request.getParameter("nickName"));
        user.setIdCardNo(request.getParameter("idCardNo"));
        user.setGender(Integer.valueOf(request.getParameter("gender")));
        user.setEmail(request.getParameter("email"));
        user.setAccount(request.getParameter("account"));

        account = userService.selectOneAccount(request.getParameter("account"));
        phone = userService.selectOnePhone(request.getParameter("phone"));

        if (account == false && phone == false) {

            insert = userService.insert(user);
        }


        JSONObject jsonObject = new JSONObject();

        if (insert > 0) {
            HttpSession httpSession = request.getSession();


            jsonObject.put("status", 1);
            jsonObject.put("message", "注册成功!");


        } else {
            jsonObject.put("status", 0);
            if (account == false && phone == false) {
                jsonObject.put("message", "注册失败!");
            } else if (account == true) {
                jsonObject.put("message", "姓名重复!");
            } else if (phone == true) {
                jsonObject.put("message", "电话重复!");
            }

        }

        try {
            response.getWriter().print(jsonObject);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
